
// *******************************
// * Raamatukogu - Ravenholm Story  *
// *******************************
// chapter1.lua

if (SERVER) then

	function STORY:Begin( ply )
		self.BaseClass.Begin( self, ply )
	
		story.LockDoor( "room1_door01" )
		story.LockDoor( "hall2_door01" )
		story.LockDoor( "hall2_door02" )
		
		local key = self:SpawnItem( "room1_floor01", "base_key" )
		key.DoorName = "room1_door01"
		
		self:SpawnItem( "room1_table01", "rh_medkit" )
		self:SpawnItem( "room2_floor02", "rh_cinderblock" )
		self:SpawnItem( "room3_table01", "rh_crowbar" )
	end

	local BeginTime = 10
	function STORY:PlayerSpawn( ply )
		self.BaseClass.PlayerSpawn( self, ply )
		ply.BeginSequence = true
		umsg.Start( "rh_start", ply )
		umsg.End()
		ply:SitFor( BeginTime+5 )
		ply.CamLocked = true
		timer.Simple( 0, function() ply:SetCamera( "room1_playercam" ) end )
		timer.Simple( 1, function() ply.CamLocked = false end )
		timer.Simple( BeginTime, function()
			self:PlayerBegin( ply )
		end )
	end
	
	local colMe = Color( 100, 100, 200 )
	function STORY:PlayerBegin( ply )
		if (!ply.BeginSequence) then return end
		ply.BeginSequence = false
		ply:ChatToSelf( "Looks to me like things getting worse not better.", 1, 3, "vo/npc/male01/question12.wav" )
		ply:ChatToSelf( "Someday this will all be a bad memory.", 4, 3, "vo/npc/male01/question20.wav" )
		timer.Simple( 5, function() ply:FadeCameras( "room1_playercam", "room1_cam", 1.5 ) end )
	end
	
	local function Skip( ply )
		if (!ply.BeginSequence) then return end
		ply:SitFor( 5 )
		ply.ActiveStory:PlayerBegin( ply )
	end
	concommand.Add( "rh_skip", Skip )
	
	local colNpc = Color( 200, 100, 100 )
	function STORY:ZombieAttack( ply )
		local door = ents.FindByName( "hall2_door03" )[1]
		local phys = door:GetPhysicsObject()
		phys:EnableMotion( true )
		local brush = ents.FindByName( "hall2_brush01" )[1]
		brush:Remove()
		timer.Simple( 0.1, function()
			local explode = ents.FindByName( "hall2_phys01" )[1]
			explode:Fire( "Explode" )
			WorldSound( "ambient/explosions/explode_1.wav", explode:GetPos(), 75, 100)
		end )
		timer.Simple( 3, function()
			WorldSound( "vo/ravenholm/firetrap_lookout.wav", ply:GetShootPos(), 75, 100 )
			// ply:ChatToSelf( "Look out brother, Behind you!", nil, 4)
			//ply:ChatMessage( colNpc, "Father Grigory
		end )
		timer.Simple( 7, function()
			WorldSound( "vo/ravenholm/aimforhead.wav", ply:GetShootPos(), 75, 100 )
			//ply:ChatToSelf( "Aim for the head!", nil, 3)
		end )
		local zombs = {
			self:SpawnNPC( "hall2_floor01", "npc_zombie" ),
			self:SpawnNPC( "hall2_floor02", "npc_zombie" ),
			self:SpawnNPC( "hall2_floor03", "npc_zombie" )
		}
		for i=1, #zombs do
			zombs[i]:SetNPCState( NPC_STATE_COMBAT )
			zombs[i]:SetTarget( ply )
			zombs[i]:SetLastPosition( ply:GetPos() )
			zombs[i]:SetSchedule( SCHED_FORCED_GO )
			zombs[i]:UpdateEnemyMemory(ply, ply:GetPos())
			zombs[i]:SetEnemy( ply)
		end
		local CheckNpcs
		function CheckNpcs()
			if ((!ValidEntity( ply )) || (!ply:Alive())) then
				return
			end
			local f
			for i=1, #zombs do
				local ent = zombs[i]
				if (ValidEntity( ent )) then f = true end
			end
			if (!f) then
				self:HandleEvent( ply, "zombie_attackfinish" )
			else
				timer.Simple( 1, CheckNpcs )
			end
		end
		timer.Simple( 1, CheckNpcs )
	end
	
	function STORY:HandleEvent( ply, event, ent )
		if (event == "usedoor") then
			if (ent.Locked) then
				if (ply:UseKey( ent:GetName() )) then
					story.UnlockDoor( ent:GetName() )
					ply:ChatPrint( "The key worked!" )
					return false
				end
				if (ent.UsedOnce) then
					ply:ChatToSelf( "It's still locked.", nil, 4 )
				else
					ply:ChatToSelf( "It's locked. I wonder if there's a key somewhere.", nil, 4 )
					ent.UsedOnce = true
				end
			end
			return true
		end
		if (event == "approach_ladder_01") then
			ply:ChatToSelf( "Seems like someone blocked off this exit." )
			return true
		end
		if (event == "approach_door_01") then
			ply:ChatToSelf( "Seems like someone blocked off this exit." )
			return true
		end
		if (event == "approach_wall_01") then
			ply:ChatToSelf( "I could probably break this if I had a sledgehammer." )
			return true
		end
		if (event == "approach_hole01") then
			ply:Give( "weapon_shotgun" )
			ply:GiveAmmo( 32, "buckshot" )
			WorldSound( "vo/ravenholm/monk_giveammo01.wav", ply:GetShootPos(), 75, 100 )
			ply:ChatToSelf( "Here, brother, Ammunition.", nil, 1)
			ply:FadeCameras( "room4_cam1", "room4_cam2", 1.5 )
			ply:SetPos( ents.FindByName( "room4_defendpoint" )[1]:GetPos() )
			ply:NoMove( true )
			ply:DefenceMode( true )
			self:ZombieAttack( ply )
			return true
		end
		if (event == "zombie_attackfinish") then
			ply:FadeCameras( "room4_cam2", "room4_cam1", 1.5 )
			ply:NoMove( false )
			ply:DefenceMode( false )
			return true
		end
		if (event == "landonfloor01") then
			local grigory = self:SpawnNPC( "room4_floor01", "rhnpc_grigory" )
			grigory:SetState( "idle_shotgun" )
			return true
		end
		if (event == "useprop") then
			local name = ent:GetName()
			if (name == "room1_cabinet01") then
				if (ent.UsedOnce) then
					ply:ChatToSelf( "It's still jammed.", nil, 4 )
				else
					ply:ChatToSelf( "It's jammed. I need a crowbar to break it open.", nil, 4 )
					ent.UsedOnce = true
				end
			end
			return false
		end
		if (event == "combineitem") then
			if (ent.Success) then
				if ((ent.ItemA.Classname == "rh_pole") && (ent.ItemB.Classname == "rh_cinderblock")) or
					((ent.ItemB.Classname == "rh_pole") && (ent.ItemA.Classname == "rh_cinderblock")) then
					
					ply:ChatToSelf( "A homemade sledge hammer. Brilliant!", nil, 4 )
				end
			else
				ply:ChatToSelf( "That would never work.", nil, 4 )
			end
			return
		end
		if (event == "meleeentity") then
			local name = ent:GetName()
			if (name == "room1_cabinet01") then
				if (ply:GetActiveWeapon():GetClass() == "rhwep_crowbar") then
					if (ent.Crowbarred) then
						ply:ChatToSelf( "I already looted this object!", nil, 4 )
					else
						ent.Crowbarred = true
						ply:FreezeNoWeps( true )
						timer.Simple( 0.25, function()
							ply:PlayPickup( ply:GetShootPos() )
						end )
						timer.Simple( 1.25, function()
							ply:ChatToSelf( "All I found was a useless wooden pole.", nil, 4 )
							local item = gstore.CreateItem( "rh_pole", ent:GetPos() )
							ply:GiveItem( item )
							ply:RefreshInv( 0.5 )
							ply:FreezeNoWeps( false )
						end )
					end
				end
			end
			if (name == "room3_breakable01") then
				if (ply:GetActiveWeapon():GetClass() == "rhwep_shammer") then
					ent:Fire( "Break" )
					timer.Simple( 0.5, ply.ChatToSelf, ply, "I wonder where this leads." )
					return true
				else
					ply:ChatToSelf( "It has no effect." )
					return false
				end
			end
			return true
		end
		if (event == "enter_room2") then
			ply:Freeze( true )
			timer.Simple( 4, ply.Freeze, ply, false )
			WorldSound( "vo/ravenholm/madlaugh01.wav", ply:GetShootPos(), 75, 100 )
			timer.Simple( 3, WorldSound, "weapons/shotgun/shotgun_fire6.wav", ply:GetShootPos(), 75, 100 )
			timer.Simple( 3.8, WorldSound, "weapons/shotgun/shotgun_fire6.wav", ply:GetShootPos(), 75, 100 )
			timer.Simple( 0.5, ply.PlaySequence, ply, "photo_react_startle", 1.5 )
			timer.Simple( 4.5, ply.ChatToSelf, ply, "Who the hell was that?", nil, 4 )
			return true
		end
		if (event == "enter_lift01") then
			ply:NoMove( true )
			local ent = ents.FindByName( "room6_lift01" )[1]
			ply:SetCamera( "room6_localcam", ent )
			timer.Simple( 1, ent.Fire, ent, "Open" )
			timer.Simple( 2, ply.NoMove, ply, false )
		end
		ply:ChatPrint( "Unhandled event! (" .. event .. ")" )
	end

end

if (CLIENT) then
	
	local StartTime
	
	local function UnMid( a, b, where )
		return math.Clamp( (where-a)/(b-a), 0, 1 )
	end
	
	local bgCol = Color( 0, 0, 0, 255 )
	local txt1 = Color( 255, 255, 255, 0 )
	local txt2 = Color( 255, 255, 255, 0 )
	function STORY:DrawHUD()
		if (!StartTime) then return end
		local prog = CurTime()-StartTime
		local w, h = ScrW(), ScrH()
		
		local bgA = 1-UnMid( StartTime+9, StartTime+10, CurTime() )
		bgCol.a = bgA*255
		surface.SetDrawColor( bgCol )
		surface.DrawRect( 0, 0, ScrW(), ScrH() )
		
		local title = self.PrintName .. " - " .. self:GetChapterName()
		
		txt1.a = UnMid( StartTime+0.5, StartTime+2, CurTime() ) * 255 * bgA
		draw.SimpleText( title, "HUDNumber", w*0.5, h*0.35, txt1, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP )
		
		txt2.a = UnMid( StartTime+2.5, StartTime+4, CurTime() ) * 255 * bgA
		draw.SimpleText( "by " .. self.Author, "Trebuchet24", w*0.5, h*0.425, txt2, TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP )
	end
	
	local function umStart( um )
		StartTime = CurTime()
		timer.Simple( 10, function() StartTime = nil end )
		surface.PlaySound( "music/ravenholm_1.mp3" )
	end
	usermessage.Hook( "rh_start", umStart )
	
end